import { gql, QueryResult, useQuery } from "@apollo/client";

import { Search, AircraftQuery } from "~/components/Home/home";
import { getContent } from "~/plugins/api-server";

// 查询分析任务列表
export const useTasksList = (params: Search) => {
  const document = gql`
    query tasks($TaskQuery: TaskQuery!) {
      tasks(query: $TaskQuery) {
        page {
          page
          pageSize
        }
        totalCount
        data {
          id
          name
          tailno
          aircraftId
          created_at
          updated_at
          start_time
          end_time
          flights
        }
      }
    }
  `;
  const query = {
    variables: {
      TaskQuery: params,
    },
    client: getContent().explorer_apiServer,
  };
  const { data, refetch } = useQuery<QueryResult<any>>(document, query);
  return { data, refetch };
};
// 查询飞机、机尾号
export const useAircraftTailNum = () => {
  const document = gql`
    query aircrafts {
      aircrafts {
        id
        tailNo
      }
    }
  `;
  const { data } = useQuery<QueryResult<any>>(document);
  return data;
};

// 查询航班
export const useAirline = (params: AircraftQuery) => {
  const document = gql`
    query aircraft($id: String!){
      aircraft(id: $id){
        id
        tailNo
        flights(CurrentYear:"${params.CurrentYear}",CurrentMonth:"${params.CurrentMonth}") {
          id
          takeoffTime
        }
      }
    }
  `;
  const query = {
    variables: {
      id: params.id,
    },
  };
  const { data } = useQuery<QueryResult<any>>(document, query);
  return data;
};
